package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {
    @Autowired
    BrandCategoryMapper brandCategoryMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void addNew(BrandCategoryAddNewDTO dto){
        log.debug("开始处理添加品牌分类业务");
        //判断品牌是否存在
        BrandStandardVO standardById
                = brandMapper.getStandardById(dto.getBrandId());
        if (standardById == null) {
            //否: 品牌不存在, 抛出异常, 品牌分类添加失败
            String message = "品牌不存在, 品牌分类添加失败";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //是: 品牌存在, 继续判断类别是否存在
        CategoryStandardVO categoryStandard
                = categoryMapper.getCategoryStandard(dto.getCategoryId());
        if (categoryStandard == null) {
            //否: 品牌不存在, 抛出异常, 品牌分类添加失败
            String message = "类别不存在, 品牌分类添加失败";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //是: 类别存在, 继续判断尝试绑定的关系是否已存在
        Long brandId = standardById.getId();
        Long categoryId = categoryStandard.getId();
        int i = brandCategoryMapper.countByCategoryIdAndBrandId(categoryId, brandId);
        if (i > 0) {
            //是: 绑定关系已存在, 品牌分类添加失败, 抛出异常
            String message = "品牌分类添加失败, 绑定关系已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //否: 可以添加品牌类别
        BrandCategory brandCategory = new BrandCategory();
        BeanUtils.copyProperties(dto, brandCategory);
        log.debug("开始添加品牌分类, 参数:{}", brandCategory);
        i = brandCategoryMapper.insert(brandCategory);
        if (i != 1) {
            String message = "品牌分类添加失败, 服务器忙, 请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long categoryId, Long brandId) {
        log.debug("开始处理[解绑品牌与类别业务]");
        //判断是否存在绑定关系
        int i = brandCategoryMapper.countByCategoryIdAndBrandId(categoryId, brandId);
        log.debug("i的值:{}", i);
        if (i == 0) {
            //否: 不存在绑定关系, 解绑失败, 抛出异常
            String message = "不存在绑定关系, 解绑失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //是: 存在绑定关系, 可以解除
        log.debug("存在绑定关系, 可以删除");
        i = brandCategoryMapper
                .deleteByCategoryIdAndBrandIdAndBrandId(categoryId, brandId);
        if (i != 1) {
            String message = "品牌分类删除失败, 服务器忙, 请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }
}
